﻿@using Microsoft.AspNetCore.Mvc.Localization
@using Reborn.IdentityServer4.Admin.UI.Configuration.Constants
@using Reborn.IdentityServer4.Admin.UI.Helpers
@model Reborn.IdentityServer4.Admin.BusinessLogic.Shared.Dtos.Common.Pager
@inject IViewLocalizer Localizer
@{
    var currentPage = PagerHelpers.GetCurrentPage(Context.Request.Query["page"]);
    var totalCount = PagerHelpers.GetTotalPages(Model.PageSize, Model.TotalCount);
    var minPage = PagerHelpers.GetMinPageToRender(Model.MaxPages, totalCount, currentPage);
    var maxPage = PagerHelpers.GetMaxPageToRender(Model.MaxPages, totalCount, currentPage);
}

@if (currentPage <= maxPage)
{
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">

            @if (Model.EnableSearch)
            {
                <li class="page-item">
                    <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="1" asp-route-search="@Model.Search">@Localizer["FirstButtonText"]</a>
                </li>
            }
            else
            {
                <li class="page-item">
                    <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="1">@Localizer["FirstButtonText"]</a>
                </li>
            }

            @if (PagerHelpers.ShowLeftPagerButton(Model.MaxPages, totalCount, currentPage))
            {
                if (Model.EnableSearch)
                {
                    <li class="page-item">
                        <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@(minPage - 1)" asp-route-search="@Model.Search">...</a>
                    </li>
                }
                else
                {
                    <li class="page-item">
                        <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@(minPage - 1)">...</a>
                    </li>
                }
            }

            @for (var i = minPage; i <= maxPage; i++)
            {
                if (Model.EnableSearch)
                {
                    if (PagerHelpers.IsActivePage(currentPage, i))
                    {
                        <li class="page-item active">
                            <a class="page-link" asp-action="@Model.Action" asp-route-page="@i" asp-route-search="@Model.Search">@i</a>
                        </li>
                    }
                    else
                    {
                        <li class="page-item">
                            <a class="page-link" asp-action="@Model.Action" asp-route-page="@i" asp-route-search="@Model.Search">@i</a>
                        </li>
                    }
                }
                else
                {
                    if (PagerHelpers.IsActivePage(currentPage, i))
                    {
                        <li class="page-item active">
                            <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@i">@i</a>
                        </li>
                    }
                    else
                    {
                        <li class="page-item">
                            <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@i">@i</a>
                        </li>
                    }
                }
            }

            @if (PagerHelpers.ShowRightPagerButton(Model.MaxPages, totalCount, currentPage))
            {
                if (Model.EnableSearch)
                {
                    <li class="page-item">
                        <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@(maxPage + 1)" asp-route-search="@Model.Search">...</a>
                    </li>
                }
                else
                {
                    <li class="page-item">
                        <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@(maxPage + 1)">...</a>
                    </li>
                }
            }

            @if (Model.EnableSearch)
            {
                <li class="page-item">
                    <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@totalCount" asp-route-search="@Model.Search">@Localizer["LastButtonText"]</a>
                </li>
            }
            else
            {
                <li class="page-item">
                    <a class="page-link" asp-area="@CommonConsts.AdminUIArea" asp-action="@Model.Action" asp-route-page="@totalCount">@Localizer["LastButtonText"]</a>
                </li>
            }
        </ul>
    </nav>
}